package app {
	import com.temple.core.CoreMovieClip;
	import flash.display.MovieClip;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author hungconcon
	 */
	
	[Embed(source="../../lib/embed/asset.swf",symbol="Character")]
	
	public class Player extends MovieClip {
		
		public var c1:MovieClip;
		public var c2:MovieClip;
		public var c3:MovieClip;
		public var c4:MovieClip;
		public var c5:MovieClip;
		
		public var pm:PlayerManager;
		
		public function Player() {
			super();
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function init(evt:Event):void {
			x = 200;
			y = 400;
			App.oldx = x;
			
			App.px = x;
			App.py = y;
			App.xs = App.pspeed;
			App.act = "stand";
			App.pymin = 10;
			App.pymax = 200;
			App.powermax = 31;
			App.powermin = 15;
			App.powerrange = App.powermax - App.powermin;
			
			
			pm = new PlayerManager();
			pm.l = [c1, c2, c3, c4, c5];
			addEventListener(Event.ENTER_FRAME, onFrame);
			
			c5.gotoAndStop(1);
			c5.addFrameScript(13, ashoot,18, c5.stop);
			
		}
		/**
		 * re-active shoot when it finished
		 */
		private function ashoot():void {
			isenter = true;
			dispatchEvent(new Event(App.ACTIVE_SHOOT_OUT));
			
			App.hold = 0;
			App.closeball = null;
		}
		/**
		 * Always keep loop going!
		 */
		public var isenter:Boolean = true;
		
		private var runtime:int = 0;
		private var moving:int = 0;
		
		public function onShoot():void {
			if (!isenter) return;
			
			App.powerdiv = (App.pymax - Math.max(App.pymin, Math.min(App.pymax, stage.mouseY))) / (App.pymax - App.pymin);
			App.p_x = x;
			App.p_y = y;
			
			c5.gotoAndStop(1);
			c5.play();
			isenter = false;
			pm.gotoAndStop("shoot");
		}
		public function onFrame(event:Event):void {
			if (!isenter) return;
			//[0]
			App.oldx = x;
			App.px = Math.max(App.xmin, Math.min(stage.mouseX, App.xmax));
			App.py = Math.max(App.ymin, Math.min(stage.mouseY, App.ymax));
			App.dx = App.px - x;
			
			//[0]
			(Math.abs(App.dx) > 1) ? ((App.dx > 0) ? (App.hd = 1) : (App.hd = 2)) : null;
			//[1]
			if (Math.abs(App.dx) > Math.abs(App.xs)) {
				runtime++;
				moving = 500;
				App.dx > 0 ? (App.nx = x + App.xs) : (App.nx = x - App.xs);
				x = App.nx;
			} else {
				if (runtime > 0) {
					x = App.px;
					moving = 0;
					runtime = 0;
				} else if (Math.abs(App.dx) > 0) {
					x = App.px;
					moving = 500;
				}
				moving > 495 ? (moving = 5) : (moving > 5 ? (moving = 0) : null);
			}
			moving > 0 ? moving-- : null;
			//[2]
			App.act = "run";
			if (Math.abs(App.oldx - App.px) < 1){
				(!moving || x == App.xmax || x == App.xmin) ? (App.act = "stand") : null;
			}
			//[2.1]
			pm.gotoAndStop(App.act + App.hold);
			//[2.2]
			(App.hd == 1) ? (scaleX = 1) : (scaleX = -1);
			
			if (App.hold) return;
			
			App.closeball = null;
			App.balldist = 1000;
			//[3]
			
			for (var i:int = 0; i < App.balls.length; i++){
				if (App.balls[i].y > 270) {
					App.dx = Math.abs(App.balls[i].x - x);
					if (App.dx < 40 && App.dx < App.balldist){
						App.balldist = App.dx;
						App.closeball = App.balls[i];
					}
				}
			}
			//[4]
			if (App.balldist < 1000){
				App.hold = 1;
				App.ballFactory.deleteBallOnList(App.closeball);
				//closeball.removeMovieClip();
				//App.zgrab.start();
				App.closeball = null;
			}
		}
	}
}